package sql.utils;

public class Utils {
    public static Operator getOperator(String op) {
        switch (op) {
            case "=":
            case "==":
                return Operator.eq;
            case ">":
                return Operator.gt;
            case ">=":
                return Operator.gte;
            case "<":
                return Operator.lt;
            case "<=":
                return Operator.lte;
            case "<>":
            case "!=":
                return Operator.ne;
            case "IN":
                return Operator.in;
            default:
                return null;
        }
    }

    public static Number parseNumber(String str) {
        // 去除字符串两端的空白字符
        String trimmed = str.trim();

        // 处理空字符串或无效输入
        if (trimmed.isEmpty()) {
            throw new NumberFormatException("Empty string");
        }

        // 检查字符串是否包含小数点
        boolean containsDecimal = trimmed.contains(".");

        // 尝试解析为整数（Long）或不含小数点的数字
        if (!containsDecimal) {
            try {
                // 先尝试解析为 Long
                long longValue = Long.parseLong(trimmed);
                // 如果值在 int 范围内，返回 Integer
                if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
                    return Integer.valueOf((int) longValue);
                }
                return Long.valueOf(longValue);
            } catch (NumberFormatException e) {
                // 如果 Long 解析失败（如超出 Long 范围），尝试用 Double 解析
                try {
                    return Double.valueOf(trimmed);
                } catch (NumberFormatException e2) {
                    throw new NumberFormatException("Invalid number format: " + str);
                }
            }
        }
        // 包含小数点时解析为 Double
        else {
            try {
                return Double.valueOf(trimmed);
            } catch (NumberFormatException e) {
                throw new NumberFormatException("Invalid decimal format: " + str);
            }
        }
    }


    public static boolean compareNum(Number a, Number b, Operator op) {
        // 处理整数类型（Byte, Short, Integer, Long）
        if (isIntegerType(a) && isIntegerType(b)) {
            long longA = a.longValue();
            long longB = b.longValue();
            return compareLongs(longA, longB, op);
        }

        // 处理浮点类型（Float, Double）和其他类型
        double doubleA = a.doubleValue();
        double doubleB = b.doubleValue();
        return compareDoubles(doubleA, doubleB, op);
    }

    // 检查是否为整数类型（Byte/Short/Integer/Long）
    private static boolean isIntegerType(Number num) {
        return num instanceof Byte ||
                num instanceof Short ||
                num instanceof Integer ||
                num instanceof Long;
    }

    // 使用 long 精确比较整数
    private static boolean compareLongs(long a, long b, Operator op) {
        switch (op) {
            case gt:  return a > b;
            case gte: return a >= b;
            case eq:  return a == b;
            case lt:  return a < b;
            case lte: return a <= b;
            case ne:  return a != b;
            default: throw new IllegalArgumentException("未知操作符: " + op);
        }
    }

    // 使用 double 比较浮点数
    private static boolean compareDoubles(double a, double b, Operator op) {
        switch (op) {
            case gt:  return a > b;
            case gte: return a >= b;
            case eq:  return a == b;
            case lt:  return a < b;
            case lte: return a <= b;
            case ne:  return a != b;
            default: throw new IllegalArgumentException("未知操作符: " + op);
        }
    }
}
